<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Generator" content="Kate, the KDE Advanced Text Editor" />
<title>Membrane.py</title>
</head>
<body>
<pre style='color:#141312;background-color:#ffffff;'>
<i><span style='color:#008000;'>&quot;&quot;&quot;********************************************************************************</span></i>
<i><span style='color:#008000;'>                            Membrane.py</span></i>
<i><span style='color:#008000;'>                 DAE Tools: pyDAE module, www.daetools.com</span></i>
<i><span style='color:#008000;'>                 Copyright (C) Dragan Nikolic, 2010</span></i>
<i><span style='color:#008000;'>***********************************************************************************</span></i>
<i><span style='color:#008000;'>DAE Tools is free software; you can redistribute it and/or modify it under the </span></i>
<i><span style='color:#008000;'>terms of the GNU General Public License as published by the Free Software </span></i>
<i><span style='color:#008000;'>Foundation; either version 3 of the License, or (at your option) any later version.</span></i>
<i><span style='color:#008000;'>The DAE Tools is distributed in the hope that it will be useful, but WITHOUT ANY </span></i>
<i><span style='color:#008000;'>WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A </span></i>
<i><span style='color:#008000;'>PARTICULAR PURPOSE. See the GNU General Public License for more details.</span></i>
<i><span style='color:#008000;'>You should have received a copy of the GNU General Public License along with the</span></i>
<i><span style='color:#008000;'>DAE Tools software; if not, write to the Free Software Foundation, Inc., </span></i>
<i><span style='color:#008000;'>59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.</span></i>
<i><span style='color:#008000;'>********************************************************************************&quot;&quot;&quot;</span></i>

<span style='color:#0000ff;'>import</span> sys
<span style='color:#0000ff;'>from</span> daetools.pyDAE <span style='color:#0000ff;'>import</span> <span style='color:#0000ff;'>*</span>
<span style='color:#0000ff;'>from</span> time <span style='color:#0000ff;'>import</span> localtime, strftime

typeNone         <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;None&quot;</span>,         <span style='color:#bf0303;'>&quot;-&quot;</span>, <span style='color:#0000ff;'>-</span><span style='color:#c000c0;'>1E20</span>, <span style='color:#0000ff;'>+</span><span style='color:#c000c0;'>1E20</span>,     <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1e-5</span>)
typeFraction     <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Fraction&quot;</span>,     <span style='color:#bf0303;'>&quot;-&quot;</span>,     <span style='color:#c000c0;'>0</span>,     <span style='color:#c000c0;'>1</span>,   <span style='color:#c000c0;'>0.1</span>, <span style='color:#c000c0;'>1e-5</span>)
typePermeability <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Permeability&quot;</span>, <span style='color:#bf0303;'>&quot;-&quot;</span>,     <span style='color:#c000c0;'>0</span>,   <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1e-10</span>, <span style='color:#c000c0;'>1e-5</span>)
typeFlowrate     <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Flowrate&quot;</span>,     <span style='color:#bf0303;'>&quot;-&quot;</span>,     <span style='color:#c000c0;'>0</span>,   <span style='color:#c000c0;'>1E6</span>,   <span style='color:#c000c0;'>1E4</span>, <span style='color:#c000c0;'>1e-5</span>)
typeArea         <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Area&quot;</span>,         <span style='color:#bf0303;'>&quot;-&quot;</span>,     <span style='color:#c000c0;'>0</span>,   <span style='color:#c000c0;'>1E6</span>,   <span style='color:#c000c0;'>1E4</span>, <span style='color:#c000c0;'>1e-5</span>)
typeLength       <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Length&quot;</span>,       <span style='color:#bf0303;'>&quot;-&quot;</span>,     <span style='color:#c000c0;'>0</span>,  <span style='color:#c000c0;'>1E10</span>,     <span style='color:#c000c0;'>1</span>, <span style='color:#c000c0;'>1e-5</span>)
typePressure     <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Pressure&quot;</span>,     <span style='color:#bf0303;'>&quot;-&quot;</span>,     <span style='color:#c000c0;'>0</span>,  <span style='color:#c000c0;'>1E10</span>,     <span style='color:#c000c0;'>1</span>, <span style='color:#c000c0;'>1e-5</span>)

<b>class</b> modMembrane(daeModel):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>None</span>, Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;&quot;</span>):
        daeModel.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent, Description)

        <span style='color:#0000ff;'>self</span>.Nc <span style='color:#0000ff;'>=</span> daeDomain(<span style='color:#bf0303;'>&quot;N_c&quot;</span>, <span style='color:#0000ff;'>self</span>)

        <span style='color:#0000ff;'>self</span>.xf <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;x_f&quot;</span>, typeFraction, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.xf.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.Nc)
        
        <span style='color:#0000ff;'>self</span>.x0 <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;x_0&quot;</span>, typeFraction, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.x0.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.Nc)
        
        <span style='color:#0000ff;'>self</span>.yp <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;y_p&quot;</span>, typeFraction, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.yp.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.Nc)
        
        <span style='color:#0000ff;'>self</span>.Perm <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;P_erm&quot;</span>, typePermeability, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.Perm.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.Nc)

        <span style='color:#0000ff;'>self</span>.L0 <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;L_0&quot;</span>, typeFlowrate, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.Vp <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;V_p&quot;</span>, typeFlowrate, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.Lf <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;L_f&quot;</span>, typeFlowrate, <span style='color:#0000ff;'>self</span>)
        
        <span style='color:#0000ff;'>self</span>.Am    <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;A_m&quot;</span>, typeArea, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.t     <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;&amp;delta;&quot;</span>,   typeLength, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.theta <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;&amp;theta;&quot;</span>,  typeFraction, <span style='color:#0000ff;'>self</span>)

        <span style='color:#0000ff;'>self</span>.Ph <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;P_h&quot;</span>, typePressure, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.Pl <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;P_l&quot;</span>, typePressure, <span style='color:#0000ff;'>self</span>)
        
    <b>def</b> DeclareEquations(<span style='color:#0000ff;'>self</span>):
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;x_f&quot;</span>)
        i <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.Nc, eClosedClosed)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.Vp() <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.yp(i) <span style='color:#0000ff;'>-</span> (<span style='color:#0000ff;'>self</span>.Perm(i) <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.Am() <span style='color:#0000ff;'>/</span> <span style='color:#0000ff;'>self</span>.t()) <span style='color:#0000ff;'>*</span> (<span style='color:#0000ff;'>self</span>.Ph() <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.x0(i) <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.Pl() <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.yp(i))

        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;x_0&quot;</span>)
        i <span style='color:#0000ff;'>=</span> eq.DistributeOnDomain(<span style='color:#0000ff;'>self</span>.Nc, eClosedClosed)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.x0(i) <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.xf(i) <span style='color:#0000ff;'>/</span> (<span style='color:#c000c0;'>1</span> <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.theta()) <span style='color:#0000ff;'>+</span> <span style='color:#0000ff;'>self</span>.yp(i) <span style='color:#0000ff;'>*</span> <span style='color:#0000ff;'>self</span>.theta() <span style='color:#0000ff;'>/</span> (<span style='color:#c000c0;'>1</span> <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.theta())

        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;sum_yp&quot;</span>)
        r <span style='color:#0000ff;'>=</span> daeIndexRange(<span style='color:#0000ff;'>self</span>.Nc)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.<b><span style='color:#000000;'>sum</span></b>(<span style='color:#0000ff;'>self</span>.yp.array(r)) <span style='color:#0000ff;'>-</span> <span style='color:#c000c0;'>1</span>
        
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;&amp;Theta;&quot;</span>)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.theta() <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.Vp() <span style='color:#0000ff;'>/</span> <span style='color:#0000ff;'>self</span>.Lf()
        
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;L_f&quot;</span>)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.Lf() <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.L0() <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.Vp()
        
<b>class</b> simMembrane(daeDynamicSimulation):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>):
        daeDynamicSimulation.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.m <span style='color:#0000ff;'>=</span> modMembrane(<span style='color:#bf0303;'>&quot;Membrane&quot;</span>)
          
    <b>def</b> SetUpParametersAndDomains(<span style='color:#0000ff;'>self</span>):
        <span style='color:#0000ff;'>self</span>.m.Nc.CreateArray(<span style='color:#c000c0;'>3</span>)

    <b>def</b> SetUpVariables(<span style='color:#0000ff;'>self</span>):
        <span style='color:#0000ff;'>self</span>.m.Perm.AssignValue(<span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>200E-10</span>)
        <span style='color:#0000ff;'>self</span>.m.Perm.AssignValue(<span style='color:#c000c0;'>1</span>, <span style='color:#c000c0;'>50E-10</span>)
        <span style='color:#0000ff;'>self</span>.m.Perm.AssignValue(<span style='color:#c000c0;'>2</span>, <span style='color:#c000c0;'>25E-10</span>)

        <span style='color:#0000ff;'>self</span>.m.xf.AssignValue(<span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>0.25</span>)
        <span style='color:#0000ff;'>self</span>.m.xf.AssignValue(<span style='color:#c000c0;'>1</span>, <span style='color:#c000c0;'>0.55</span>)
        <span style='color:#0000ff;'>self</span>.m.xf.AssignValue(<span style='color:#c000c0;'>2</span>, <span style='color:#c000c0;'>0.20</span>)

        <span style='color:#0000ff;'>self</span>.m.Lf.AssignValue(<span style='color:#c000c0;'>1E4</span>)
        <span style='color:#0000ff;'>self</span>.m.theta.AssignValue(<span style='color:#c000c0;'>0.25</span>)
        <span style='color:#0000ff;'>self</span>.m.t.AssignValue(<span style='color:#c000c0;'>2.54E-3</span>)
        <span style='color:#0000ff;'>self</span>.m.Ph.AssignValue(<span style='color:#c000c0;'>300</span>)
        <span style='color:#0000ff;'>self</span>.m.Pl.AssignValue(<span style='color:#c000c0;'>30</span>)

        <i><span style='color:#008000;'>#self.m.Am.SetInitialGuess(1E6)</span></i>
        
        <i><span style='color:#008000;'>#self.m.yp.SetInitialGuess(0, 0.4555)</span></i>
        <i><span style='color:#008000;'>#self.m.yp.SetInitialGuess(1, 0.4410)</span></i>
        <i><span style='color:#008000;'>#self.m.yp.SetInitialGuess(2, 0.0922)</span></i>
        
        <i><span style='color:#008000;'>#self.m.x0.SetInitialGuess(0, 0.18)</span></i>
        <i><span style='color:#008000;'>#self.m.x0.SetInitialGuess(1, 0.58)</span></i>
        <i><span style='color:#008000;'>#self.m.x0.SetInitialGuess(2, 0.23)</span></i>

<i><span style='color:#008000;'># Create Log, Solver, DataReporter and Simulation object</span></i>
log          <span style='color:#0000ff;'>=</span> daePythonStdOutLog()
solver       <span style='color:#0000ff;'>=</span> daeIDASolver()
datareporter <span style='color:#0000ff;'>=</span> daeTCPIPDataReporter()
simulation   <span style='color:#0000ff;'>=</span> simMembrane()

<i><span style='color:#008000;'># Enable reporting of all variables</span></i>
simulation.m.SetReportingOn(<span style='color:#0000ff;'>True</span>)

<i><span style='color:#008000;'># Set the time horizon and the reporting interval</span></i>
simulation.ReportingInterval <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>1</span>
simulation.TimeHorizon <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>1</span>

<i><span style='color:#008000;'># Connect data reporter</span></i>
simName <span style='color:#0000ff;'>=</span> simulation.m.Name <span style='color:#0000ff;'>+</span> strftime(<span style='color:#bf0303;'>&quot; [</span><span style='color:#0000ff;'>%d</span><span style='color:#bf0303;'>.%m.%Y %H:%M:%S]&quot;</span>, localtime())
<b>if</b>(datareporter.Connect(<span style='color:#bf0303;'>&quot;&quot;</span>, simName) <span style='color:#0000ff;'>==</span> <span style='color:#0000ff;'>False</span>):
    sys.exit()

simulation.Initialize(solver, datareporter, log)
daeSaveModel(simulation.m, simulation.m.Name <span style='color:#0000ff;'>+</span> <span style='color:#bf0303;'>&quot;.xml&quot;</span>)

<i><span style='color:#008000;'># Solve at time=0 (initialization)</span></i>
simulation.SolveInitial()
<i><span style='color:#008000;'># Run</span></i>
simulation.Run()

</pre>
</body>
</html>
